import { FormSchema } from '@/components/Table';
import { selectUserByRole } from '@/api/applay/trainlist/self'
import { ref,computed } from 'vue'
import dayjs from 'dayjs'
const trainingTime = ref<any>("")
//新增和编辑培训
export const a =computed(()=>{
  return trainingTime.value
})
export const addTrain: FormSchema[] = [
  {
    field: 'trainingStartTime',
    label: '培训时间：',
    component: 'DatePicker',
    componentProps: {
      'valueFormat': "YYYY-MM-DD",
      disabledDate(current) {
        return current && current < dayjs().endOf('day');
      },
      onChange: (value) => {
        trainingTime.value = value
      },
    },
    rules: [
      {
        required: true,
        message: '请选择培训时间',
      },
    ],
  },
  {
    field: 'trainingUserIds',
    label: '培训官：',
    component: 'ApiSelect',
    componentProps: {
      api: selectUserByRole,
      params: {
        request: 'TRAINING',
        type: '0'
      },
      allowClear: false,
      mode: 'multiple', // 设置为多选模式
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      },
    },
    rules: [
      {
        required: true,
        message: '请选择培训官',
      },
    ],

  },
  {
    field: 'teacherUserIds',
    label: '培训导师：',
    component: 'ApiSelect',
    componentProps: {
      api: selectUserByRole,
      params: {
        request: 'TRAINER',
        type: '0',
      },
      allowClear: false,
      mode: 'multiple', // 设置为多选模式
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      }
    },
  },
  {
    field: 'resultNoticeTime',
    label: '录取结果通知时间：',
    component: 'DatePicker',
    componentProps: {
      valueFormat: "YYYY-MM-DD HH:mm",
      format: "YYYY-MM-DD HH:mm",
      showTime: { format: 'HH:mm' },
      disabledDate(current) {
        return current && current < dayjs(trainingTime.value).endOf("day");
      },
      
    },
    dynamicRules: () => {
      return [
        {
          required: true,
          validator: (_, value) => {
            if (!value) {
              return Promise.reject('请选择录取结果通知时间')
            }
            const trainingEndTime: any = trainingTime.value;
            if (trainingEndTime && dayjs(value).isBefore(trainingEndTime)) {
              return Promise.reject('录取结果通知时间不得早于培训时间')
            }
            return Promise.resolve()
          },
        },
      ];
    },
    rules: [
      {
        required: true,
        message: '请选择培训时间',
      },
    ],
  },
  // {
  //   field: 'examineStartTime',
  //   label: '考核时间：',
  //   component: 'DatePicker',
  //   componentProps: {
  //     valueFormat: 'YYYY-MM-DD',
  //     disabledDate(current) {
  //       const trainingStartTime = trainingTime.value[0]
  //         ? trainingTime.value[0]
  //         : null;
  //       if (!trainingStartTime) return false
  //       const twoYearsLater = dayjs(trainingStartTime).add(2, 'years')
  //        // 禁用日期规则
  //       return current && (current.isBefore(trainingStartTime) || current.isAfter(twoYearsLater))
  //     },
  //   },
  //   dynamicRules: () => {
  //     return [
  //       {
  //         required: true,
  //         validator: (_, value) => {
  //           if (!value) {
  //             return Promise.reject('请选择考核时间')
  //           }
  //           const trainingEndTime = trainingTime.value[1] || ''
  //           if (trainingEndTime && dayjs(value).isBefore(trainingEndTime)) {
  //             return Promise.reject('考核时间不得早于培训结束时间')
  //           }
  //           return Promise.resolve()
  //         },
  //       },
  //     ];
  //   },
  // },
]

//批量操作
export const batchAddTrain: FormSchema[] = [
  {
    field: 'trainingUserIds',
    label: '培训官：',
    component: 'ApiSelect',
    componentProps: {
      api: selectUserByRole,
      params: {
        request: 'TRAINING',
        type: '0'
      },
      allowClear: false,
      mode: 'multiple', // 设置为多选模式
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      }
    },
    rules: [
      {
        required: true,
        message: '请选择培训官',
      },
    ],
  },
  {
    field: 'teacherUserIds',
    label: '培训导师：',
    component: 'ApiSelect',
    componentProps: {
      api: selectUserByRole,
      params: {
        request: 'TRAINING',
        type: '0',
      },
      allowClear: false,
      mode: 'multiple', // 设置为多选模式
      filterOption: (input: string, option: any) => {
        return option.label.toLowerCase().indexOf(input.toLowerCase()) >= 0;
      }
    },
  },
]